<?php

namespace core\libs;

class config {
    static public $config_cache = [];
    static public function get($file, $name = "") {
        if(isset(self::$config_cache[$file])) {
            if($name) {
                $temp = self::$config_cache[$file];
                $arr_name = explode('->', $name);
                foreach($arr_name as $val) {
                    if(isset($temp[$val])) {
                        $temp = $temp[$val];
                    }
                    else{
                        throw new \Exception("没有这个配置项" . $name);
                    }
                }
                return $temp;
            }
            else {
                return self::$config_cache[$file];
            }
        }
        else {
            $path = APP_PATH . 'core/config/' . $file . '.php';
            if(is_file($path)) {
                $result = include $path;
                if($name) {
                    $temp = $result;
                    $arr_name = explode('->', $name);
                    foreach($arr_name as $val) {
                        if(isset($temp[$val])) {
                            $temp = $temp[$val];
                        }
                        else{
                            throw new \Exception("没有这个配置项" . $name);
                        }
                    }

                    self::$config_cache[$file] = $result;
                    return $temp;
                }
                else {
                    self::$config_cache[$file] = $result;
                    return $result;
                }
            }
            else {
                throw new \Exception("配置文件不存在" . $path);
            } 
        }
    }
}